import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';

@Component({
  selector: 'app-changshangmsg',
  templateUrl: './changshangmsg.component.html',
  styleUrls: ['./changshangmsg.component.scss']
})
export class ChangshangmsgComponent implements OnInit {
  isShow; // 新增
  isVisible = false;
  listOfData;
  editCache = {};
  fullname; // 廠商全名
  tax_rate; // 稅率

  constructor(public axios: AxiosService) { }

  ngOnInit(): void {
    this.getData();
  }

  // 查询信息
  getData() {
    this.axios.axiosPost('QueryFirmInfo')
      .then((data) => {
        if (data) {
          console.log(data);
          this.listOfData = data['order'];
          for (let i = 0; i < this.listOfData.length; i++) {
            this.editCache[i] = { edit: true, isdel: true, data: this.listOfData[i] };
          }
          console.log(this.listOfData);
          console.log(this.editCache);
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }

  // 修改信息
  startEdit(key: number): void {
    console.log(key)
    console.log(this.editCache[key])
    this.editCache[key].isdel = false;
    this.editCache[key].edit = false;
  }
  // 保存修改
  saveEdit(key: number): void {
    this.editCache[key].isdel = true;
    this.editCache[key].edit = true;
    this.axios.axiosPost('AddFirmInfo', {
      fullname: this.editCache[key]['data'].fullname,
      tax_rate: this.editCache[key]['data'].tax_rate
    })
      .then((data) => {
        if (data['result'] === 'ok') {
          console.log(data)
          this.getData();
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }
  // 撤销修改
  cancelEdit(key: number): void {
    // debugger
    // const index = this.listOfData.findIndex((item, index) => index === key);
    // this.editCache[key] = {
    //   data: { ...this.listOfData },
    //   edit: true,
    //   isdel: true
    // };
    this.getData();
  }

  // 新增
  showinput() {
    this.isShow = true;
    this.fullname = '';
    this.tax_rate = '';
  }
  // 判断厂名是否存在，若已存在读取信息到input中
  pushFullname() {
    for (let i = 0; i < this.listOfData.length; i++) {
      if (this.fullname === this.listOfData[i]['fullname']) {
        this.tax_rate = this.listOfData[i]['tax_rate']
      }
    }
  }
  // 确认新增
  confirmAdd() {
    if (this.fullname !== '' && this.tax_rate !== '') {
      this.axios.axiosPost('AddFirmInfo', {
        fullname: this.fullname,
        tax_rate: this.tax_rate
      })
        .then((data) => {
          if (data['result'] === 'ok') {
            console.log(data)
            this.getData();
          }
        })
        .catch((err) => {
          console.log(err);
        })
      this.isShow = false;
    } else {
      alert('廠商全稱和稅率不能為空');
    }
  }
  // 取消新增
  cancelAdd() {
    this.isShow = false;
  }

  // 删除
  showModal(item): void {
    this.fullname = item.fullname
    console.log(this.fullname)
    this.isVisible = true;
  }
  handleOk() {
    this.isVisible = false;
    // debugger
    this.axios.axiosPost('DeleteFirmInfo', { 'fullname': this.fullname })
      .then((data) => {
        console.log(data)
        if (data['result'] === 'ok') {
          this.getData();
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }
  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

}
